﻿#region using
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using System.Data;
using System.Text;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Threading;
using System.Globalization;
using Microsoft.Win32;
using System.Net.Mail;
using System.Data.Sql;
using System.Diagnostics;
#endregion
namespace newTurtle8
{

    public class CompanyManager
    {
        #region definitions
        private static CompanyManager instance = new CompanyManager();
        internal SqlManager sqlManager;
        public static readonly string StartupPath = System.IO.Directory.GetCurrentDirectory();
        
        #endregion

        #region Constructors
        private CompanyManager()
        {
            validateSingletonProcess();
            //if (getValueFromInitFile("IsInitialized").Equals("false"))
               // new Initialization(sqlManager);
            sqlManager = new SqlManager();

            changeLangToHebrew();
        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainMenu());
        }

        #region static methods


        //Singleton
        public static CompanyManager getInstance() { return instance; }

        /// <summary>
        /// a parser for company.ini initialization file.
        /// </summary>
        /// <param name="key"></param>
        /// <returns>the right value of the key
        ///          example: IsInitialized=false. the function retrurns "false";
        /// </returns>
        static internal string getValueFromConfigFile(string key)
        {
            // create reader & open file
            TextReader tr = new StreamReader(@"other\company.ini");
            string line;
            string value = string.Empty;

            while ((line = tr.ReadLine()) != null)
                if (line.StartsWith("<" + key))
                {
                    line = line.Substring(line.IndexOf("=") + 1);
                    value += line;

                    while ((!line.EndsWith(">") && (line = tr.ReadLine()) != null))
                        value += line;

                    value = value.Substring(0, value.Length - 1);
                    break;
                }

            tr.Close();
            return value.Trim();
        }

        static public byte[] imageToByteArray(System.Drawing.Image imageIn)
        {
            if (imageIn == null)
                return null;
            MemoryStream ms = new MemoryStream();
            imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

            return ms.ToArray();
        }

        static public Image byteArrayToImage(byte[] byteArrayIn)
        {
            if (byteArrayIn == null)
                return null;
            MemoryStream ms = new MemoryStream(byteArrayIn);
            Image returnImage = Image.FromStream(ms);
            return returnImage;
        }

        #endregion

        /// <summary>
        /// validates that only 1 process instance runs at a time on the same machine.
        /// </summary>
        private void validateSingletonProcess()
        {
            string currentName = Process.GetCurrentProcess().ProcessName;
            Process currentProcess = Process.GetCurrentProcess();
            Array arr = Process.GetProcesses();

            foreach (Process item in arr)
            {
                if (item.ProcessName.Equals(currentName) && !item.Id.Equals(currentProcess.Id))
                    currentProcess.Kill();
            }
            string processName = System.Diagnostics.Process.GetCurrentProcess().ProcessName;
        }

        static public void changeLangToHebrew()
        {
            InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(new System.Globalization.CultureInfo("he"));
        }

        static public void changeLangToEnglish()
        {
            InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(new System.Globalization.CultureInfo("en"));
        }
    }
}
